﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SacNetUG.Models;

namespace SacNetUG.Controllers
{
    [Authorize]
    public partial class SponsorCategoryController : Controller
    {
        public virtual ActionResult Index()
        {
            return View(repository.Db.Table<SponsorCategory>().OrderBy(e => e.SortOrder));
        }

        public virtual ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Create(SponsorCategory sponCat)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(sponCat);

                repository.Db.InsertOnSubmit(sponCat);
                repository.Db.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public virtual ActionResult Edit(int id)
        {
            if (id == 0) return View(Views.Create);

            return View(repository.Db.Table<SponsorCategory>().FirstOrDefault(p => p.SponsorCategoryId == id));
        }

        [HttpPost]
        public virtual ActionResult Edit(int id, SponsorCategory sponCat)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(sponCat);

                UpdateModel(repository.Db.Table<SponsorCategory>().FirstOrDefault(p => p.SponsorCategoryId == id));
                repository.Db.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
